<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\BrandModel;
use App\Models\ManufacturerModel;
use App\Models\MaterialInquiryModel;
use App\Models\InvoiceCategoryModel;
use App\Models\MaterialModel;
use App\Models\SupplierModel;
use Illuminate\Http\Request;

class MaterialInquiryController extends Controller
{
    //询价记录列表
    public function listing(Request $request)
    {
        $page = $request->post('page', 1);
        $take = $request->post('take', 10);

        $seacher_title = $request->post('seacher_title');

        $material_type = $request->post('material_type');

        $material_code = $request->post('material_code');

        $supplier_id = $request->post('supplier_id');

        $invoice_type = $request->post('invoice_type');

        $start_time = $request->post('start_time');

        $end_time = $request->post('end_time');

        $data = MaterialInquiryModel::when($seacher_title,function($query,$seacher_title){
                return  $query->where('material_name','like','%'.$seacher_title.'%')->orWhere('material_code','%'.$seacher_title.'%');
        })->when($material_code,function($query,$material_code){
                return  $query->where('material_code',$material_code);
        })->when($material_type,function($query,$material_type){
            return  $query->where('material_type',$material_type);
        })->when($supplier_id,function($query,$supplier_id){
            return  $query->where('supplier_id',$supplier_id);
        })->when($invoice_type,function($query,$invoice_type){
            return  $query->where('invoice_type',$invoice_type);
        })->when($start_time,function($query,$start_time){
            return  $query->where('inquiry_time','>=',date('Y-m-d H:i:s',$start_time));
        })->when($end_time,function($query,$end_time){
            return  $query->where('inquiry_time','=<',date('Y-m-d H:i:s',$end_time));
        })->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        foreach($data as $key=>$val){
            $data[$key]['invoice_type_name'] = InvoiceCategoryModel::selectValue('id',$val['invoice_type']);
        }

        return response()->json(['code'=>'200','msg' => '提交成功','data'=>$data]);
    }

    /**
     * 添加询价记录
     */
    public function add(Request $request){

        $member_sub = response()->json(auth('api')->user());

        $member_list = $member_sub->original;

        $material_id = $request->post('material_id');

        $supplier_id = $request->post('supplier_id');

        $brand = $request->post('brand');

        $manufacturer = $request->post('manufacturer');

        $inquiry_time = $request->post('inquiry_time');

        $invoice_type = $request->post('invoice_type');

        $supplier_offer = $request->post('supplier_offer');

        $remark = $request->post('remark');

        $material_list = MaterialModel::where(['id'=>$material_id])->select(['material_code','material_name'])->first();

        $data = [
            'brand' => $brand,
            'manufacturer' => $manufacturer,
            'inquiry_time' => date('Y-m-d',$inquiry_time),
            'invoice_type' => $invoice_type,
            'supplier_offer' => $supplier_offer,
            'remark' => $remark,
            'material_code' => $material_list['material_code'],
            'material_name' => $material_list['material_name'],
            'supplier_name' => SupplierModel::selectValue('id',$supplier_id),
            'admin_id' => $member_list['id']
        ];
        MaterialInquiryModel::insert($data);

        if(!empty($manufacturer)){
            $data = ManufacturerModel::where(['material_id'=>$material_id,'manufacturer_name'=>$manufacturer])->first();
            if(empty($data)){
                ManufacturerModel::insert([
                    'material_id' => $material_id,
                    'manufacturer_name' => $manufacturer,
                    'admin_id'=> $member_list['id'],
                    'status' => 1,
                    'created_at'=> date('Y-m-d H:i:s',time()),
                    'updated_at'=> date('Y-m-d H:i:s',time()),
                ]);
            }
        }
        if(!empty($brand)){
            $data = BrandModel::where(['material_id'=>$material_id,'brand_name'=>$brand])->first();
            if(empty($data)){
                BrandModel::insert([
                    'material_id' => $material_id,
                    'brand_name' => $brand,
                    'admin_id'=> $member_list['id'],
                    'status' => 1,
                    'created_at'=> date('Y-m-d H:i:s',time()),
                    'updated_at'=> date('Y-m-d H:i:s',time()),
                ]);
            }
        }

        return response()->json(['code'=>'200','msg' => '请求成功']);

    }
}
